/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scheduleManager;

import app.Global;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import lib.JButtonTable.ButtonEditor;

/**
 *
 * @author yoshi
 */
public class AssignHeadButtonEditor extends ButtonEditor {

    public AssignHeadButtonEditor(JCheckBox checkBox, final AbstractScheduleManager myView) {
        super(checkBox);
        
        final AssignHeadButtonEditor self = this;
        
        this.button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                AssignHeadButtonModel model = (AssignHeadButtonModel)self.getCellEditorValue();
                if(model.getSchedule() == null){
                    new AddScheduleDialog(Global.getFrame(), true, myView, model.getDate(), model.getTimeSlot(), myView.getMyModel().getEmployees()).setVisible(true);
                }else{
                    if(JOptionPane.showConfirmDialog(Global.getFrame(), "Are you sure to delete this time slot?", "Delete schedule", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
                        ((AbstractScheduleManager)myView).getMyController().deleteSchedule(model.getSchedule());
                        ((AbstractScheduleManager)myView).refresh();
                    }
                }
            }
        });
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        button.setText(((AssignHeadButtonModel) value).getText());
        return super.getTableCellEditorComponent(table, value, isSelected, row, column);
    }
    
}
